﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.WinAPI;
using System.Threading;

namespace fhjlForm
{
    public partial class Form_Main : Form
    {
        public bool threadrun;
        public bool recvdata;
        public Thread thread;//阻塞模式读取数据线程
        public int serv_start = 0;
        public void tingzhifuwu()
        {
            StringBuilder mess = new StringBuilder(1000);
            //停止服务
            HongDaGPRS.do_close_all_user2(mess);//开发包函数，使所有DTU下线
            if (Properties.Settings.Default.ServerMode != 2)
                GPRSTimer.Enabled = false;
            if (Properties.Settings.Default.ServerMode == 0)
            {
                HongDaGPRS.cancel_read_block();//阻塞模式下取消阻塞读取
                threadrun = false;//退出线程处理函数
                thread.Abort();//终止线程
            }
            if (HongDaGPRS.stop_net_service(mess) == 0)//开发包函数，停止服务
            {
                //界面处理
                serv_start = 0;
                toolStripStatusLabel1.Text = "GPRS服务停止";
            }
            AddText(mess.ToString());
        }
        public void qidongfuwu()
        {
            int sign = 9999;
            HongDaGPRS.SetCustomIP(Win32API.inet_addr(Properties.Settings.Default.ServerIP));
            HongDaGPRS.SetWorkMode(Properties.Settings.Default.ServerMode);//开发包函数，设置服务模式：2-消息，0-阻塞，1-非阻塞
            HongDaGPRS.SelectProtocol(Properties.Settings.Default.ServerType);//开发包函数，设置服务类型：0-UDP，1-TCP
            StringBuilder mess = new StringBuilder(1000);
            if (Properties.Settings.Default.ServerMode == 2)
            {
                sign = HongDaGPRS.start_net_service(this.Handle, HongDaGPRS.WM_DTU, Properties.Settings.Default.ServerPort, mess);//开发包函数，消息模式启动服务
            }
            else
            {
                sign = HongDaGPRS.start_net_service(this.Handle, 0, Properties.Settings.Default.ServerPort, mess);//开发包函数，非消息模式启动服务
                GPRSTimer.Interval = 100;
                GPRSTimer.Enabled = true;//启动非消息模式下数据读取和处理定时器
            }
            if (Properties.Settings.Default.ServerMode == 0)
            {
                GPRSTimer.Enabled = false;
                threadrun = true;
                recvdata = false;
                thread = new Thread(new ThreadStart(pressdata));//创建新的阻塞模式下读取数据线程
                thread.Start(); //启动阻塞模式下读取数据线程
            }
            if (sign == 0)
            {
                serv_start = 1;
                toolStripStatusLabel1.Text = "GPRS服务启动";
                if (Properties.Settings.Default.ServerType == 0)
                {
                    if (Properties.Settings.Default.ServerMode == 0) AddText("UDP:阻塞模式");
                    else if (Properties.Settings.Default.ServerMode == 1) AddText("UDP:非阻塞模式");
                    else if (Properties.Settings.Default.ServerMode == 2) AddText("UDP:消息模式");
                }
                else if (Properties.Settings.Default.ServerType == 1)
                {
                    if (Properties.Settings.Default.ServerMode == 0) AddText("TCP:阻塞模式");
                    else if (Properties.Settings.Default.ServerMode == 1) AddText("TCP:非阻塞模式");
                    else if (Properties.Settings.Default.ServerMode == 2) AddText("TCP:消息模式");
                }
            }
            else
            {
                serv_start = 0;
                toolStripStatusLabel1.Text = "GPRS未服务启动";
            }
            AddText(mess.ToString() + "\r");
        }
        //********************************************************
        //阻塞模式下线程处理函数
        //********************************************************
        protected void pressdata()
        { 
            GPRS_DATA_RECORD recdPtr = new GPRS_DATA_RECORD();
            StringBuilder mess = new StringBuilder(100);
            GPRS_USER_INFO user_info = new GPRS_USER_INFO();
            //读取DTU数据				
            while (threadrun)
            {
                if (recvdata)
                    continue;
                if (threadrun == false)
                    break;
                if (HongDaGPRS.do_read_proc(ref recdPtr, mess, Properties.Settings.Default.ServerReply) >= 0)
                {
                    //byte a = recdPtr.m_data_type;
                    recvdata = true;
                    switch (recdPtr.m_data_type)
                    {
                        case 0x01:	//注册包												
                            if (HongDaGPRS.get_user_info(recdPtr.m_userid, ref user_info) == 0)
                            {
                                //已经注册过	
                                AddText(recdPtr.m_userid + "---注册");                                
                                //没有注册过
                            }                            
                            break;
                        case 0x02:	//注销包
                            AddText("\r" + recdPtr.m_userid + "---注销" + "\r\n");                            
                            break;
                        case 0x04:	//无效包
                            break;
                        case 0x09:	//数据包
//                             if (checkBox1.Checked)
//                                 AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
//                                     + StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len) + "\r\n");
//                             else
                                AddText(recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
                                    + System.Text.Encoding.Default.GetString(recdPtr.m_data_buf));
                            break;
                        case 0x0d:
                            AddText(recdPtr.m_userid + "---参数设置成功");
                            break;
                        case 0x0b:
                            AddText(recdPtr.m_userid + "---参数查询成功");
                            break;
                        case 0x06:
                            AddText(recdPtr.m_userid + "---断开PPP连接成功");
                            break;
                        case 0x07:
                            AddText(recdPtr.m_userid + "---停止向DSC发送数据");
                            break;
                        case 0x08:
                            AddText(recdPtr.m_userid + "---允许向DSC发送数据");
                            break;
                        case 0x0A:
                            AddText(recdPtr.m_userid + "---丢弃用户数据");
                            break;
                        default:
                            break;
                    }
                    recvdata = false;
                }
            }
            //	thread.Abort();
        }
        //*******************************************
        //消息模式时定义消息的响应函数
        //*******************************************
        protected override void WndProc(ref Message m)
        {
            // 
            //int i;
            //响应DTU消息
            if (m.Msg == HongDaGPRS.WM_DTU)
            {

                GPRS_DATA_RECORD recdPtr = new GPRS_DATA_RECORD();
                StringBuilder mess = new StringBuilder(100);
                //开发包函数，读取DTU数据				
                if (HongDaGPRS.do_read_proc(ref recdPtr, mess, Properties.Settings.Default.ServerReply) >= 0)
                {
                    byte a = recdPtr.m_data_type;
                    switch (recdPtr.m_data_type)
                    {
                        case 0x01:	//注册包												
                            GPRS_USER_INFO user_info = new GPRS_USER_INFO();
                            //ushort usPort;
                            if (HongDaGPRS.get_user_info(recdPtr.m_userid, ref user_info) == 0)//开发包函数，通过ID获取DTU信息
                            {                                
                                AddText(recdPtr.m_userid + "---成功注册");
                            }
                            break;
                        case 0x02:	//注销包
                            AddText(recdPtr.m_userid + "---注销");                            
                            break;
                        case 0x04:	//无效包
                            break;
                        case 0x09:	//数据包
                            //16进制显示收到的数据
//                             if (checkBox1.Checked)
//                             {
// 
//                                 //  AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
//                                 //  + StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len) + "\r\n");
// 
//                                 //  chulidata(recdPtr.m_userid, StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len));
//                                 chuligprsdata(recdPtr.m_userid, StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len));
// 
//                             }
//                             else
                                //显示数据
                                AddText(recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
                                    + System.Text.Encoding.Default.GetString(recdPtr.m_data_buf));
                            break;
                        case 0x0d:
                            //收到参数设置成功数据包，取消参数设置超时定时器
                            AddText(recdPtr.m_userid + "---参数设置成功");
                            break;
                        case 0x0b:
                            //收到查询参数数据包，取消参数查询超时定时器，并读取各项参数到DEMO
                            AddText(recdPtr.m_userid + "---参数查询成功");
                            break;
                        case 0x06:
                            AddText(recdPtr.m_userid + "---断开PPP连接成功");
                            break;
                        case 0x07:
                            AddText(recdPtr.m_userid + "---停止向DSC发送数据");
                            break;
                        case 0x08:
                            AddText(recdPtr.m_userid + "---允许向DSC发送数据");
                            break;
                        case 0x0A:
                            AddText(recdPtr.m_userid + "---丢弃用户数据");
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                //缺省消息处理
                base.WndProc(ref m);
            }
        }
        private void GPRSTimer_Tick(object sender, EventArgs e)
        {
            GPRS_DATA_RECORD recdPtr = new GPRS_DATA_RECORD();
            StringBuilder mess = new StringBuilder(100);
            //开发包函数，读取DTU数据				
            if(Properties.Settings.Default.ServerMode==1)
            {
                if (HongDaGPRS.do_read_proc(ref recdPtr, mess, Properties.Settings.Default.ServerReply) >= 0)
                {
                    byte a = recdPtr.m_data_type;
                    switch (recdPtr.m_data_type)
                    {
                        case 0x01:	//注册包												
                            GPRS_USER_INFO user_info = new GPRS_USER_INFO();
                            //ushort usPort;
                            if (HongDaGPRS.get_user_info(recdPtr.m_userid, ref user_info) == 0)//开发包函数，通过ID获取DTU信息
                            {
                                AddText(recdPtr.m_userid + "---成功注册");
                            }
                            break;
                        case 0x02:	//注销包
                            AddText(recdPtr.m_userid + "---注销");
                            break;
                        case 0x04:	//无效包
                            break;
                        case 0x09:	//数据包
                            //16进制显示收到的数据
                            //                             if (checkBox1.Checked)
                            //                             {
                            // 
                            //                                 //  AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
                            //                                 //  + StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len) + "\r\n");
                            // 
                            //                                 //  chulidata(recdPtr.m_userid, StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len));
                            //                                 chuligprsdata(recdPtr.m_userid, StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len));
                            // 
                            //                             }
                            //                             else
                            //显示数据
                            AddText(recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
                                + System.Text.Encoding.Default.GetString(recdPtr.m_data_buf));
                            break;
                        case 0x0d:
                            //收到参数设置成功数据包，取消参数设置超时定时器
                            AddText(recdPtr.m_userid + "---参数设置成功");
                            break;
                        case 0x0b:
                            //收到查询参数数据包，取消参数查询超时定时器，并读取各项参数到DEMO
                            AddText(recdPtr.m_userid + "---参数查询成功");
                            break;
                        case 0x06:
                            AddText(recdPtr.m_userid + "---断开PPP连接成功");
                            break;
                        case 0x07:
                            AddText(recdPtr.m_userid + "---停止向DSC发送数据");
                            break;
                        case 0x08:
                            AddText(recdPtr.m_userid + "---允许向DSC发送数据");
                            break;
                        case 0x0A:
                            AddText(recdPtr.m_userid + "---丢弃用户数据");
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}
